package com.example.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bo.StudentsPageBO;
import com.example.demo.entity.Students;
import com.example.demo.mapper.StudentsMapper;
import com.example.demo.service.IStudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import springfox.documentation.schema.Collections;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-10-15
 */
@Service
@Slf4j
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements IStudentsService {

    @Override
    public Page<Students> pages(StudentsPageBO studentsPageBO) {
        Page<Object> objectPage = new Page<>(studentsPageBO.getPage(), studentsPageBO.getLimit());
        Page<Students> pages = baseMapper.pages(objectPage, studentsPageBO);
        List<Students> records = pages.getRecords();
        List<Students> studentsListss = new ArrayList<>();

        log.warn("-- --------------------------------------------------------------------------------------------  ");

        List<Students> collect = records;
        List<Students> studentsList = new ArrayList<>();
        Map<Students, Students> objectObjectHashMap = new HashMap<>();
        for (int i = 0; i < collect.size(); i++) {
            Students students = collect.get(i);
            List<Students> collect1 = collect.stream().filter(dto -> !dto.getStuId().equals(students.getStuId())).collect(Collectors.toList());
            if (collect1.contains(students)) {
                if (objectObjectHashMap.containsKey(students)) {
                    Students students1 = objectObjectHashMap.get(students);
                    if (students.getStuId() > students1.getStuId()) {
                        objectObjectHashMap.put(students, students);
                    }
                } else {
                    objectObjectHashMap.put(students, students);
                }
//                    log.warn(String.format("## 有重复的对象 ## %s", students.toString()));
            } else {
//                    log.warn(String.format("##  是唯一有对象 ## %s", students.toString()));
                studentsList.add(students);
            }

        }

        for (Map.Entry<Students, Students> studentsStudentsEntry : objectObjectHashMap.entrySet()) {
            studentsList.add(studentsStudentsEntry.getValue());
//                log.warn("--   重复的ID最大的值:{}", studentsStudentsEntry.getValue().toString());
        }
        for (Students students : studentsList) {
            log.info("--   重复的ID最大的值:{}", students);

        }


        log.warn("-- --------------------------------------------------------------------------------------------  ");
        Map<Integer, String> collect1 = records.stream().filter(sdsds -> sdsds.getTeacherId().equals("32")).collect(Collectors.toMap(Students::getAge, Students::getName));
        Map<Students, Students> collectss = records.stream().collect(Collectors.toMap(asdad -> asdad, dto -> dto, (dto1, dto2) -> {
            if (dto1.getStuId() > dto2.getStuId()) {
                return dto1;
            } else {
                return dto2;
            }
        }));


        for (Map.Entry<Students, Students> studentsStudentsEntry : collectss.entrySet()) {
            studentsListss.add(studentsStudentsEntry.getValue());
            log.warn("--   重复的ID最大的值:{}", studentsStudentsEntry.getValue());


        }
        return pages;
    }
}
